<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>
        <input type="file" name="file" onchange="uploadFile(this);" >
    </div>
    <div>
        <pre id="ret-content"></pre>
    </div>
</body>
<script>
    function uploadFile(t){
        if(t.files.length <= 0) return ;
        let fm = new FormData();//初始化一个表单数据对象，在html页面中
        fm.append('file',t.files[0]);
        fetch('/upload',{    //发起请求
            //和upload.js连接就用data,和app.js 连接就用upload
            method: 'POST',
            body:fm
        })
        .then(async res => {
            if(res.ok){
                return res.text();
            }
            throw new Error(`${res.status} ${res.statusText}`)
        })
        .then(text => {
            let d = document.getElementById('ret-content');
            d.innerHTML = `<img src="/images/${text}" style="width:80%;height:auto">`;
            //src这里会向浏览器发起get请求
        })
        .catch(err => {
            console.error(err.message);
        })
        ;
    }
</script>
</html>